package gui;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.Main;
import client.ReportControl;

public class InvRepGUI extends JFrame {
	
	Object[][] data;
	String[] colInv={"Report ID","Item Name","Color","Quantity","Date"};
	String[] month;
	
	JPanel panel;
	
	JButton btnBack;
	JButton btnLogOut;
	
	JComboBox cmbMonth;
	
	JLabel lblHeader;
	JLabel lblMonth;
	
	JScrollPane scrReport;
	JTable tbl;
	
	public InvRepGUI()
	{
		super();
		init();
		
	}
	

	JLabel getLabelMonth()
	{
		if(lblMonth==null)
		{
			lblMonth=new JLabel();
			lblMonth.setText("Select Month:");
			lblMonth.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
			lblMonth.setBounds(new Rectangle(370, 110, 115, 20));
		}
		return lblMonth;
	}
	
	JScrollPane getSatTable()
	{
		data=ReportControl.mngInvRep(cmbMonth.getSelectedItem().toString());
		DefaultTableModel model = new DefaultTableModel(data, colInv) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		      return false;
		      }
		  };
		  tbl=new JTable(model);
		  scrReport=new JScrollPane(tbl);
		  scrReport.setBounds(100, 150, 800, 140);
		  return scrReport;
	}
	
	JComboBox getComboMonth()
	{
		if(cmbMonth==null)
		{
			cmbMonth=new JComboBox();
			cmbMonth.setBounds(new Rectangle(515, 110, 115, 20));
			for(int i=1;i<=12;i++)
			{
				cmbMonth.addItem(i);
			}
			cmbMonth.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					panel.remove(scrReport);
					panel.add(getSatTable());
					panel.updateUI();
				}
			});
			
			
		}
		return cmbMonth;
	}
	
	
	JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Back");
			btnBack.setBounds(880, 20, 100, 40);
		}
		return btnBack;
	}
	
JButton getBtnLogOut() {
		
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.setText("LogOut");
			btnLogOut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
					{
						dispose();
						Main.main.logOut.doClick();
					}
				}
			});
		}
		return btnLogOut;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Inventory Report");
			lblHeader.setBounds(405, 40, 190, 30);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(1000,400);
			panel.setLayout(null);
			
			panel.add(getLabelHeader());
			panel.add(getLabelMonth());
			
			panel.add(getButtonBack());
			
			panel.add(getComboMonth());
			panel.add(getSatTable());
			panel.add(lblMonth, null);
			panel.add(getBtnLogOut());
			
		}
		return panel;
	}
	
	void init()
	{
		this.setBounds((int)Main.dim.getWidth()/2-500, (int)Main.dim.getHeight()/2-275, 1000, 400);
		this.setTitle("Zer Lah - Inventory Report");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}
}
